package org.elanfox.intellicommunity.intellicommunityadmin.ws;

import lombok.extern.slf4j.Slf4j;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author allan
 * @version 1.0.0
 * @date 2019/12/24
 */
@Slf4j
public class WsMsgServer {
    public static final ConcurrentHashMap<String, WsMsgServer> CHANNELS = new ConcurrentHashMap<>();
    private Session session;
    private String cid;

    @OnOpen
    public void onOpen(Session session, @PathParam("cid") String cid) {
        this.session = session;
        this.cid = cid;
        CHANNELS.put(cid, this);
        log.info("有新窗口开始监听：cid:" + cid);
        this.session.getAsyncRemote().sendText(cid + "已监听");

    }

    @OnClose
    public void onClose() {
        CHANNELS.remove(cid);
    }

    @OnMessage
    public void onMessage(String message, Session session) {

    }
}
